#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>
#include <arpa/inet.h>
#include "safecalls.h"
#include "networkinglib.h"

#define PROTOCOL "tcp"
#define REQUEST "GET / HTTP/1.0\n\n"

int main(int argc, char *argv[]) {
  int sockid, numbytes;
  char buf[250];
  char *words[3];
  char *port = "80";

  if (argc < 3) {
    printf("Usage: webclient host path [port]\n");
  }

  if (argc == 4) {
    port = argv[3];
  }

  sockid = clientconnect(argv[1], port, PROTOCOL);

  /* The channel for communication to the server has now been established.
     Now, request the document  */

  sprintf(buf, "GET %s HTTP/1.0\n\n", argv[2]);
  write_buffer(sockid, buf, strlen(buf));

  /* Request has been sent.  Read the result and send all data to stdout. */

  copy(sockid, STDOUT_FILENO, 0);

  close(sockid);

  return 0;
}
